import axios from 'axios'
import store from '@/store'
import Vue from 'vue'
import { getToken } from '@/utils/auth'
import { Notify } from 'vant'

Vue.use(Notify)

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: process.env.VUE_APP_BASE_API,
  // 超时
  timeout: 30000
})
// request拦截器
service.interceptors.request.use(
  config => {
    if (config.data.userKey) {
      config.headers.userKey=config.data.userKey
    }
    return config
  },
  error => {
    console.log(error)
    Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(res => {
  const code = res.data.responseCode
  if(code==100){
  	return res.data
  }else{
  	 if(res.data.messageList[0]){
    	 alert(res.data.messageList[0].message)   	 	
  	 }else{
  	 	 alert('未知错误')
  	 }   	
  	 return Promise.reject('error')
  }
},
error => {
  alert('err' + error)
  return Promise.reject(error)
}
)

export default service
